package com.google.code.cisx.client.gadgets.deprecated;

import com.google.code.cisx.client.model.Gadget;
import com.google.code.cisx.client.model.GadgetClass;
import com.google.code.cisx.client.view.EditLabel;
import com.google.code.cisx.client.view.GadgetContainerView;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.Text;

public class ToDoListGadget extends Gadget {

  public static class Class extends GadgetClass {
    public Class() {
      super("To Do List", false);
    }

    public Gadget newGadget() {
      return new ToDoListGadget(this);
    }
  }

  private VerticalPanel list = new VerticalPanel();

  protected ToDoListGadget(Class c) {
    super(c);
    initWidget(list);
    list.setWidth("100%");
    Hyperlink newToDo = new Hyperlink("New To Do", "");
    newToDo.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        addToDo("New To Do " + list.getWidgetCount(), true, false);
        getGadgetListener().onInterfaceChange();
      }
    });
    list.add(newToDo);
  }

  public void addToDo(String text, boolean edit, boolean complete) {
    HorizontalPanel row = new HorizontalPanel();
    final CheckBox checkBox = new CheckBox();
    final EditLabel label = new EditLabel(text);
    final PushButton delete = new PushButton(
        GadgetContainerView.titleBarImages.closeIcon().createImage());
    checkBox.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        if (checkBox.isChecked())
          label.addStyleName("ToDoList-Done");
        else
          label.removeStyleName("ToDoList-Done");
        getGadgetListener().onInterfaceChange();
      }
    });
    delete.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        list.remove(delete.getParent());
        getGadgetListener().onInterfaceChange();
      }
    });

    row.add(checkBox);
    row.add(label);
    row.add(delete);
    row.setCellWidth(label, "100%");
    list.insert(row, list.getWidgetCount() - 1);

    if (complete) {
      checkBox.setChecked(true);
      label.addStyleName("ToDoList-Done");
    }
    if (edit) {
      label.edit();
    }
  }

  public void toXML(Element element) {
    super.toXML(element);
    for (int i = 0; i < list.getWidgetCount() - 1; i++) {
      HorizontalPanel row = (HorizontalPanel) list.getWidget(i);
      CheckBox checkBox = (CheckBox) row.getWidget(0);
      EditLabel label = (EditLabel) row.getWidget(1);
      Element toDoItemElement = element.getOwnerDocument().createElement("ToDoItem");
      toDoItemElement.setAttribute("complete", checkBox.isChecked() ? "yes" : "no");
      Text prefText = element.getOwnerDocument().createTextNode(label.getText());
      toDoItemElement.appendChild(prefText);
      element.appendChild(toDoItemElement);
    }
  }

  public void fromXML(Element element) {
    super.fromXML(element);
    NodeList toDoItems = element.getElementsByTagName("ToDoItem");
    for (int i = 0; i < toDoItems.getLength(); i++) {
      Element toDoItemElement = (Element) toDoItems.item(i);
      boolean complete = toDoItemElement.getAttribute("complete").compareTo("yes") == 0;
      Text toDoItemValue = (Text) toDoItemElement.getChildNodes().item(0);
      addToDo(toDoItemValue.getNodeValue(), false, complete);
    }
  }

}
